package cn.jit.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.jit.po.User;
import cn.jit.util.ResponseObject;
import cn.jit.util.ResponseUtil;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/file")
public class FileController extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@RequestMapping(value = "/img/upload", method = RequestMethod.GET)
	public String uploadImgPage() {

		return "file_upload";
	}

	@RequestMapping(value = "/img/back_upload", method = RequestMethod.POST)
	@ResponseBody
	public String upload(@RequestParam("image") MultipartFile image, 
							HttpServletRequest request, 
							HttpSession session) throws IOException {
		
		 String path = request.getSession().getServletContext().getRealPath("resources/upload");
		 System.out.println("存放的path：" + path);
		 System.out.println("File.separator:" + File.separator); // 结果是 \
		 String fileName = image.getOriginalFilename();
		 File dir = new File(path, fileName);
		
		 if (!dir.exists()) {
			 System.out.println("目录不存在！要准备创建！");
			 dir.mkdirs();
		 }
		 
		 image.transferTo(dir);
		 
		 return request.getContextPath() + "/upload/";
	}

	// String path =
	// request.getSession().getServletContext().getRealPath("upload");
	// System.out.println(path);
	//
	// String fileName = image.getOriginalFilename();
	// File dir = new File(path, fileName);
	//
	// if (!dir.exists()) {
	// System.out.println("目录不存在！要准备创建！");
	// dir.mkdirs();
	// }
	//
	// image.transferTo(dir);

	// ResponseObject responseObject = new ResponseObject();
	// System.out.println("进入uploadImg()");
	// //DiskFileUpload
	// ServletFileUpload servletFileUpload = new ServletFileUpload();
	//
	// //得到上传文件的保存目录，将上传的文件存放于WEB-INF目录下，不允许外界直接访问，保证上传文件的安全
	// String savePath =
	// request.getSession().getServletContext().getRealPath("/WEB-INF/resources/upload");
	// File file = new File(savePath);
	// System.out.println(savePath);
	//
	// //判断上传文件的保存目录是否存在
	// if (!file.exists() && !file.isDirectory()) {
	// System.out.println("目录不存在!");
	// file.mkdir();
	// }
	//
	// if (!ServletFileUpload.isMultipartContent(request)) {
	// // 按照传统方式获取数据
	// responseObject.setCode(500);
	// responseObject.setIsSuccess(false);
	// responseObject.setMsg("查询成功");
	// responseObject.setData(new Object());
	// // 转成JSON
	// JSONObject jsonObject = JSONObject.fromObject(responseObject);
	// // 输出
	// ResponseUtil.writeJson(response, jsonObject);
	// return;
	// }
	//
	// responseObject.setCode(200);
	// responseObject.setIsSuccess(true);
	// responseObject.setMsg("查询成功");
	// responseObject.setData(new Object());
	// // 转成JSON
	// JSONObject jsonObject = JSONObject.fromObject(responseObject);
	// // 输出
	// ResponseUtil.writeJson(response, jsonObject);
	// return;
}
